package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.AnswerComment;
import cn.wolfcode.luowowo.comment.repository.AnswerCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.IAnswerCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.mongodb.operation.OrderBy;
import javassist.runtime.Desc;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;

import java.util.List;
import java.util.Optional;

@Service
public class AnswerCommentServiceImpl implements IAnswerCommentService {

    @Autowired
    private AnswerCommentMongoRepository repository;


    public List<AnswerComment> findById(String qId) {
        List<AnswerComment> list = repository.findAllByqId(qId, Sort.by(Sort.Direction.DESC,"thumbupnum"));
        return list;
    }

    public AjaxResult saveOrUpdate(AnswerComment answer) {
        AjaxResult result = new AjaxResult();
        try {
            repository.save(answer);
            result.setData(answer);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return  result;
    }

    public void updateStats(AnswerComment answerComment) {
        repository.save(answerComment);
    }

    public List<AnswerComment> listByUserId(Long userId) {

        return repository.findAllByUserId(userId);
    }

    public List<AnswerComment> findByUserId(Long id) {
        return repository.findByUserId(id);
    }


}
